// Tags:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

const int N = 1e3 + 5, shf = N * 5, inf = 0x3f3f3f3f;
int n, f[N][shf << 1];
short a[N], b[N];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P1282 多米诺骨牌.in", "r", stdin);
  freopen("P1282 多米诺骨牌.out", "w", stdout);
#endif
#endif

  read(n);
  for (int i = 1; i <= n; ++i) { read(a[i]), read(b[i]); }
  memset(f, inf, sizeof(f));
  f[0][shf] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < shf << 1; ++j) {
      if (j >= a[i] - b[i] && j - a[i] + b[i] < (shf << 1))
        f[i][j] = std::min(f[i][j], f[i - 1][j - a[i] + b[i]]);
      if (j >= b[i] - a[i] && j - b[i] + a[i] < (shf << 1))
        f[i][j] = std::min(f[i][j], f[i - 1][j - b[i] + a[i]] + 1);
    }
  }
  int ans;
  for (int i = shf; ~i; --i) {
    if (f[n][i] != inf) {
      ans = std::min(f[n][i], f[n][(shf << 1) - i]);
      break;
    }
  }
  printf("%d", ans);
  return 0;
}